package com.paziy.card_games.cards;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: paziy
 * Date: 15.02.13
 * Time: 11:09
 * To change this template use File | Settings | File Templates.
 */
public class CardsFactory {
    private CardsFactory(){}

    /**
     * All instances of created cards
     */
    private static Map<Suit, Map<Nominal, Card>> cards = new HashMap<Suit, Map<Nominal, Card>>();

    /**
     * Create instances of Card or get an existing Card.
     * In one time can by only one instance of Card with same suit and nominal or no one.
     * @param suit is suit of Card
     * @param nominal is nominal of Card
     * @return instance of Card with given suit and nominal
     */
    public static Card getCard(Suit suit, Nominal nominal) {
        Map<Nominal, Card> suitMap;

        if (cards.containsKey(suit)) {
            suitMap = cards.get(suit);

            if (!suitMap.containsKey(nominal)) {
                suitMap.put(nominal, new SimpleCard(suit, nominal));
            }
        }
        else {
            suitMap = new HashMap<Nominal, Card>();
            suitMap.put(nominal, new SimpleCard(suit, nominal));
            cards.put(suit, suitMap);
        }

        return cards.get(suit).get(nominal);
    }

    public static void clear() {
        cards.clear();
    }

    public static void main(String[] args) {
        getCard(Suit.SPADE,Nominal.NINE);
        getCard(Suit.SPADE,Nominal.NINE);
        getCard(Suit.SPADE,Nominal.SIX);
        System.out.println(cards);
    }
}
